//
//  PPTimelineAlbumActivityObject.m
//  pickpic
//
//  Created by Jinxiao on 10/5/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPTimelineAlbumActivityObject.h"

@implementation PPTimelineAlbumActivityObject

+ (instancetype)activityForParameters:(NSDictionary *)parameters {

    PPTimelineAlbumActivityObject *activity = [[PPTimelineAlbumActivityObject alloc] init];

    NSString *type = [parameters validStringForKey:@"type"];

    if([type isEqualToString:@"AlbumCreate"]) {
        activity.activityType = PPTimelineAlbumActivityTypeCreate;
    } else if([type isEqualToString:@"PictureAdded"]) {
        activity.activityType = PPTimelineAlbumActivityTypeAdded;
    } else if([type isEqualToString:@"AlbumUpdate"]) {
        activity.activityType = PPTimelineAlbumActivityTypeUpdate;
    }

    activity.version = [parameters validStringForKey:@"version"];

    activity.date = [NSDate dateWithTimeIntervalSince1970:[parameters validNumberForKey:@"createDate"].integerValue/1000.0];

    activity.brief = [parameters validStringForKey:@"content"];

    NSDictionary *sourceAlbum = [parameters validDictionaryForKey:@"sourceAlbum"];
    if(sourceAlbum != nil) {
        PPAlbumObject *album = [PPAlbumObject albumForParameters:sourceAlbum];
        activity.album = album;
    }

    NSDictionary *sourceUser = [parameters validDictionaryForKey:@"sourceUser"];
    if(sourceUser != nil) {
        PPTimelineUserObject *user = [PPTimelineUserObject userForParameters:sourceUser];
        activity.user = user;
    }

    NSArray *addedPictures = [parameters validArrayForKey:@"addedPictures"];
    if(addedPictures != nil) {
        NSMutableArray *pictures = [NSMutableArray array];
        for(NSDictionary *parameters in addedPictures) {
            PPTimelinePictureObject *picture = [PPTimelinePictureObject pictureForParameters:parameters];
            [pictures addObject:picture];
        }
        activity.pictures = pictures;
    }

    return activity;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeInteger:self.activityType forKey:@"activityType"];
    [encoder encodeObject:self.date forKey:@"date"];
    [encoder encodeObject:self.brief forKey:@"brief"];
    [encoder encodeObject:self.album forKey:@"album"];
    [encoder encodeObject:self.user forKey:@"user"];
    [encoder encodeObject:self.pictures forKey:@"pictures"];
    [encoder encodeObject:self.version forKey:@"version"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];

    if(self) {
        self.activityType = [decoder decodeIntegerForKey:@"activityType"];
        self.date = [decoder decodeObjectForKey:@"date"];
        self.brief = [decoder decodeObjectForKey:@"brief"];
        self.album = [decoder decodeObjectForKey:@"album"];
        self.user = [decoder decodeObjectForKey:@"user"];
        self.pictures = [decoder decodeObjectForKey:@"pictures"];
        self.version = [decoder decodeObjectForKey:@"version"];
    }

    return self;
}

@end
